package hu.uniobuda.nik.bsik2f.FileBrowser;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Matusinka Roland
 * Date: 2013.03.25.
 * Time: 22:14
 */

class Utils {

    public static File[] sortByName(File[] input) {

        if (input != null) {
            List<File> files = new ArrayList<File>();
            List<File> dirs = new ArrayList<File>();
            for (File f : input) {
                if (f.isFile()) files.add(f);
                if (f.isDirectory()) dirs.add(f);
            }
            Collections.sort(dirs, new SortFileName());
            Collections.sort(files, new SortFileName());
            List<File> list = new ArrayList<File>();
            list.addAll(dirs);
            list.addAll(files);
            return list.toArray(input);
        } else return null;
    }

    public static String getExtension(String name) {
        String ext = null;
        int i = name.lastIndexOf('.');
        if (i > 0 && i < name.length() - 1) ext = name.substring(i + 1).toLowerCase();
        return ext;
    }

}

class SortFileName implements Comparator<File> {
    @Override
    public int compare(File f1, File f2) {
        return f1.getName().compareTo(f2.getName());
    }
}